<?php
namespace app\index\controller;

use app\common\controller\FrontBase;
use think\Request;
use app\common\service\User as UserService;

class User extends FrontBase{

    /**
     *
     */
    public function register(){

        return view('',$this->param);
    }


    /**
     * 注册用户的默认角色应该是可以在后台配置的
     * @param Request $request
     */
    public function createUser(Request $request){
        if(!$request->isPost()){
            $this->error("来源非法");
        }

        $input = $request->only(['name','account','password','repassword']);

        if($input['password'] != $input['repassword']){
            $this->error("两次密码不一致");
        }
        unset($input['repassword']);

        $input['role_id'] = 1;//注册用户-这个是配置的 目前这里写死
        $userService = new UserService();
        $result = $userService->insert($input);

        if(1 == $result['state']){
            $this->success("注册成功",'/index/user/door');
        }else{
            $this->error($result['message']);
        }
    }

    public function door(){

        return view('',$this->param);
    }

    public function login(Request $request){
        if(!$request->isPost()){
            $this->error("来源非法");
        }
        $input = $request->only(['account','password']);
        $userService = new UserService();
        $result = $userService->login($input['account'],$input['password']);
        if($result){
            $this->success("登录成功");
        }
        $this->error("登录失败");
    }

    public function logout(){
        $userService = new UserService();

        $userService->logout();

        $this->success('退出成功');
    }
}